package com.mooc.ppjoke.ui.home;

import android.os.Bundle;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.paging.ItemKeyedDataSource;
import androidx.paging.PagedList;
import androidx.paging.PagedListAdapter;

import com.mooc.libnavannoation.FragmentDestination;
import com.mooc.ppjoke.data.Feed;
import com.mooc.ppjoke.exoplayer.PageListPlayDetector;
import com.mooc.ppjoke.ui.AbListFragment;
import com.mooc.ppjoke.ui.MutableDataSource;
import com.scwang.smartrefresh.layout.api.RefreshLayout;

import org.jetbrains.annotations.NotNull;

import java.util.List;

@FragmentDestination(pageUrl = "main/tabs/home", asStarter = true)
public class HomeFragment extends AbListFragment<Feed, HomeViewModel> {

    private String feedType;
    private PageListPlayDetector pageListPlayDetector;

    public static HomeFragment newInstance(String feedType) {

        Bundle args = new Bundle();
        args.putString("feedType", feedType);
        HomeFragment fragment = new HomeFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void afterViewCreated() {
        mViewModel.getCacheLVData().observe(this, feeds -> adapter.submitList(feeds));

        pageListPlayDetector = new PageListPlayDetector(this, rv);

    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if(hidden){
            pageListPlayDetector.onPause();
        }else {
            pageListPlayDetector.onResume();
        }
    }
    

    @Override
    public PagedListAdapter getAdapter() {
        feedType = getArguments() == null ? "all" : getArguments().getString("feedType");
        return new FeedAdapter(getContext(), feedType) {
            @Override
            public void onViewAttachedToWindow2(@NonNull @NotNull ViewHolder holder) {
                if (holder.isVideoItem()) {
                    pageListPlayDetector.addTarget(holder.getNetPlayerView());
                }
            }

            @Override
            public void onViewDetachedFromWindow2(@NonNull @NotNull ViewHolder holder) {
                pageListPlayDetector.removeTarget(holder.getNetPlayerView());

            }
        };
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        bind = null;

    }


    @Override
    public void onLoadMore(@NonNull @NotNull RefreshLayout refreshLayout) {
        final PagedList<Feed> currentList = adapter.getCurrentList();
        if (currentList == null || currentList.size() <= 0) {
            finishRefresh(false);
            return;
        }
        Feed feed = currentList.get(adapter.getItemCount() - 1);
        //todo
        if (currentList.size() <= 0) {
            finishRefresh(false);
            return;
        }


        mViewModel.loadAfter(feed.id, new ItemKeyedDataSource.LoadCallback<Feed>() {
            @Override
            public void onResult(@NonNull @NotNull List<Feed> data) {
                PagedList.Config config = adapter.getCurrentList().getConfig();
                if (data != null && data.size() > 0) {
                    MutableDataSource dataSource = new MutableDataSource();
                    dataSource.data.addAll(data);
                    PagedList pagedList = dataSource.buildPaagedList(config);
                    submitList(pagedList);
                }
            }
        });
    }

    @Override
    public void onRefresh(@NonNull @NotNull RefreshLayout refreshLayout) {
        mViewModel.getDataSource().invalidate();
        refreshLayout.finishRefresh();
    }

    @Override
    public void onPause() {
        pageListPlayDetector.onPause();
        Log.i("=====home", "onPause: feedtyep"+feedType);
        super.onPause();
    }

    @Override
    public void onResume() {
        pageListPlayDetector.onResume();
        if(getParentFragment()!=null){
            if(getParentFragment().isVisible()){
                Log.i("=====home", "resume: feedtyep"+feedType);
                pageListPlayDetector.onResume();
            }
        }else {
            if(isVisible()){
                Log.i("=====home", "resume: feedtyep"+feedType);
                pageListPlayDetector.onResume();
            }
        }
        super.onResume();
    }
}